package hunterxhunter.greedisland.entities.sprite;

import hunterxhunter.greedisland.data.AnimationSequence;
import hunterxhunter.greedisland.data.enums.Direction;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
import framework.eea.StateBasedGame;
import framework.eea.gui.Graphics;
import framework.eea.gui.JFrameGameContainer;
import framework.eea.util.Timer;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.World;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class AnimatedBodyEntity extends BodyEntity {

    private Map<String, AnimationSequence> animations;
    private Timer animationTimer;
    private AnimationSequence currentAnimation;
    private boolean animate;
    private Direction direction;
    
    private boolean showBorder;
    
    private Timer emoteTimer;
    private boolean showEmote;
    private String emoteString;
    
    private int defaultVelocity = 50;
    private int defaultAnimationSpeed = 100;
    
    public AnimatedBodyEntity(String id, BodyType type, Point position, Dimension size, float friction, float density, World world) {
        super(id, type, position, size, friction, density, world);
        animations = new HashMap<String, AnimationSequence>();
    }

    public void move(Direction dir) {
        move(dir, getDefaultVelocity(), getDefaultAnimationSpeed());
    }
    
    public void move(Direction dir, int velocity, int animationSpeed) {
        if(dir == Direction.NO) {
            if(direction == null) {
                direction = Direction.DOWN;
            }
            freeze(direction.toAnimation(), 0);
        } else {
            animate(dir.toAnimation(), animationSpeed);
        }
        getBody().setLinearVelocity(dir.toVec2().mul(velocity));
        direction = dir;
    }
    
    public void stand() {
        move(Direction.NO, 0, 0);
    }
    
    public void showEmote(String emote, int wait) {
        emoteTimer = new Timer(wait);
        showEmote = true;
        emoteString = emote;
    }
    
    public void animate(String name, int delta) {
        if(animations.containsKey(name)) {
            animationTimer = new Timer(delta);
            currentAnimation = animations.get(name);
            animate = true;
        }
    }
    
    public void freeze(String name, int index) {
        if(animations.containsKey(name)) {
            currentAnimation = animations.get(name);
            currentAnimation.setCurrent(index);
            animate = false;
        }
    }
    
    @Override
    public void render(JFrameGameContainer gc, StateBasedGame sb, Graphics g) {
        
        //bounding box
        if(isShowBorder()) {
            g.setColor(Color.white);
            g.drawRect(this);
        }
        
        //inc frame
        if(currentAnimation != null && animate && animationTimer != null && animationTimer.check()) {
            currentAnimation.inc();
        }
        
        //draw animation frame
        if(currentAnimation != null)
            g.drawImage(currentAnimation.getImageRef(), this, currentAnimation.getCurrentRect());
        
        //emotes
        if(showEmote) {
            g.drawImage("/greedisland/img/gon.png", new Rectangle(getPosition().x + 15, getPosition().y - 35, 35, 22), new Rectangle(366, 23, 35, 22));
            
            g.setColor(Color.black);
            g.drawStringHoriCenter(emoteString, getPosition().x + 33, getPosition().y - 21, 9);
            
            if(emoteTimer.check())
                showEmote = false;
        }
    }

    
    
    
    /**
     * @return the animations
     */
    public Map<String, AnimationSequence> getAnimations() {
        return animations;
    }

    
    
    /**
     * @return the defaultVelocity
     */
    public int getDefaultVelocity() {
        return defaultVelocity;
    }

    /**
     * @param defaultVelocity the defaultVelocity to set
     */
    public void setDefaultVelocity(int defaultVelocity) {
        this.defaultVelocity = defaultVelocity;
    }

    /**
     * @return the defaultAnimationSpeed
     */
    public int getDefaultAnimationSpeed() {
        return defaultAnimationSpeed;
    }

    /**
     * @param defaultAnimationSpeed the defaultAnimationSpeed to set
     */
    public void setDefaultAnimationSpeed(int defaultAnimationSpeed) {
        this.defaultAnimationSpeed = defaultAnimationSpeed;
    }

    /**
     * @return the showBorder
     */
    public boolean isShowBorder() {
        return showBorder;
    }

    /**
     * @param showBorder the showBorder to set
     */
    public void setShowBorder(boolean showBorder) {
        this.showBorder = showBorder;
    }
    
}
